//= encoding UTF-8
//= require enjoyhint-3.1.0-master/src/_enjoyhint.js
//= require enjoyhint-3.1.0-master/src/_jquery.enjoyhint.js
//= require konva-1.4.0/_konva.js
//= require jquery.scrollTo-2.1.2/_jquery.scrollTo.js

if (window.o == undefined) window.o = {}

/**
 * 新手向导。
 */
o.tourtip = {

	pluginIns: undefined,			// 插件实例
	code: '',						// code标识
	tourtipUrl: "/secUser/tourtip",	// url地址
	selectors: [],					// 向导的所有选择器

	/**
	 * @private
	 * 初始化向导内容
	 * @see CommonTagLib.tourtip
	 */
	init: function (code, options) {
		if (!options) return;	// 提示错误信息？
		this.code = code;
		// 初始向导
		initEnjoyHint(options, this);

		/** 初始化enjoyhint插件 */
		function initEnjoyHint(options, op) {
			op.pluginIns = new EnjoyHint({
				onStart: op.onStart.bind(op),
				onEnd: op.onEnd.bind(op)
			});

			var enjoyhint_script_steps = [];
			$.each(options, function (key, option) {
				var option = options[key], opt = {}
				op.selectors.push(key);
				if (typeof(option) == 'string') {
					opt[' ' + key] = option;
				} else {
					opt[' ' + key] = option.description;
				}
				opt['nextButton'] = {text: m('jquery.tourtip.button.next')};
				opt['skipButton'] = {text: m('jquery.tourtip.button.close')};
				opt['showNext'] = true;
				opt['showSkip'] = true;
				enjoyhint_script_steps.push(opt);
			});
			enjoyhint_script_steps[enjoyhint_script_steps.length - 1].showNext = false;

			op.pluginIns.set(enjoyhint_script_steps);
			if (op.pluginIns.getCurrentStep() != 0)
				op.pluginIns.trigger('skip');
		}
	},

	/**
	 * @public
	 * 打开向导页面。非强制显示情况下，只显示一次。
	 *
	 * @param force    是否强制显示
	 */
	start: function (force) {
		this.force = force;
		this._start(0);
	},

	/**
	 * @public
	 * 显示指定步骤的向导页面。
	 *
	 * @param selector/step    选择器或步骤序号（从0开始计数）
	 */
	show: function (selector) {
		this.force = true;
		var step = typeof(selector) == "string" ? this.selectors.indexOf(selector) : selector;
		this._start(step);
	},

	/**
	 * @private
	 */
	_start: function (step) {
		if (!this.pluginIns) return;
		if (!step || step < 0) step = 0;
		try {
			//$('body').css({'overflow': 'hidden'});
			this.pluginIns.run(step);
		} catch (e) {
			window.console.log("tourtip.start:" + e);
		}
	},

	/**
	 * @public
	 * 关闭向导。
	 */
	close: function () {
		try {
			if (!this.pluginIns) return;
			$('.enjoyhint_skip_btn').click();
		} catch (e) {
			window.console.log("tourtip.close:" + e);
		}
	},

	/**
	 * @private
	 * 开始事件：验证是否满足显示条件
	 */
	onStart: function () {
		if (!this.force && page && page.controller != "login") {
			var result,
				url = page.contextPath + this.tourtipUrl + "?command=hasTourtip&code=" + this.code
					+ "&_=" + parseInt(new Date().getTime() / 10000);
			$.ajaxSetup({async: false});
			$.getJSON(url, function (data) {
				result = data.result;
			});
			if (result) {
				//$('body').css({'overflow': 'auto'});
				throw 'false';
			}
		}
	},

	/**
	 * @private
	 * 结束事件：记录已提示状态
	 */
	onEnd: function () {
		if (page && page.controller != "login") {
			var url = page.contextPath + this.tourtipUrl;
			$.post(url, {command: "noMoreTourtip", code: this.code});
		}
	}
	
};